<?php

namespace Admin\Controller;

class GroupOrderController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("GroupOrder");
    }

    //首页
    public function index() {

        $requestMap = $this->convertRequestToMap();
//        if($requestMap["o#status"] == -2){
//            unset($requestMap["o#status"]);
////            $requestMap["o#status-neq"] = array(1,2);
//            $requestMap["cp#used"] = 0;
//        }
        if(!isNullOrEmpty($requestMap["o#create_time-elt"])){
            $requestMap["o#create_time-elt"] .= " 23:59:59";
        }
        unset($requestMap["pageSize"]);
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
//        $requestMap["fo.enterprise_code"] = $user["enterprise_code"];
        $this->assign("begin_time", $_REQUEST["o#create_time-egt"]);
        $this->assign("end_time", $_REQUEST["o#create_time-elt"]);

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $pageSize = $_REQUEST["pageSize"];
        if(isNullOrEmpty($pageSize)){
            $pageSize = 20;
            $page = $this->page($count, $pageSize);
        }else{
            if($pageSize == -1){
                $page = $this->page($count, 0);
            }else{
                $page = $this->page($count, $pageSize);
            }
        }
        $this->assign('pageSize', $pageSize);

        $orderList = $this->currentModel->queryForList($page, $requestMap);

        $this->assign("user", $user);
        $this->assign("defaultEnum", C("INVOICE_TYPE"));
        $this->assign("statusEnum", C("GROUP_ORDER_STATUS"));
        $this->assign('orderList', $orderList);
        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }

    public function getTable()
    {
//        $data = self::getData();
        $ids = $_REQUEST["id"];
        $map['class_name'] = "reporting_group_order";

        $formDict = D("FormDict")->queryForList($map);

        $data = array();
        $data["header"] = array();
        $data["volume"] = array();
        $data["list"] = array();
        foreach ($formDict as $key => $value){
            array_push($data["header"], $value);
            array_push($data["volume"], $key);
        }

//        $users_obj= M("admin");
//        $userId=$_SESSION['ADMIN_ID'];
//        $user=$users_obj->where("user_id=$userId")->find();

        //获取列表
        $list = D("GroupOrder")->alias("go")
        ->join("left join __ORDER__ o on o.order_id = go.order_id")
        ->join("left join __PRODUCT__ p on p.product_id = o.product_id")
        ->join("left join __CUSTOMER__ cus on cus.customer_id = o.customer_id")
        ->order("go.group_order_id asc")
        ->field("go.*, p.product_id, p.product_name, p.image_url, cus.nickname, p.group_price, o.price, o.size, o.number, o.status, o.patient_name, o.pay_time, o.need_invoice, o.invoice_name, o.invoice_number, o.receive_name, o.receive_contact, o.receive_address,o.express_company, o.delivery_number, o.outstock_time, o.note")
        ->select();

//        $ids = $_REQUEST["id"];
        $idArr = explode(',', $ids);
        $numberSum = 0;
        $totalPriceSum = 0;
        $sumName = "product_name";
        $numberName = "number";
        $priceName = "group_price";
        foreach ($list as $item){
            if(in_array($item["group_order_id"], $idArr)){
                $item["size"] = "[" . $item["size"] ."]";
                array_push($data["list"], $item);
                $numberSum += $item[$numberName];
                $totalPriceSum += $item[$priceName];
            }
        }

        $newItem = $list[0];
        foreach ($newItem as $key=>$value){
            if($key == $numberName)
                $newItem[$key] = $numberSum;
            elseif($key == $priceName)
                $newItem[$key] = $totalPriceSum;
            else
                $newItem[$key] = "";
        }
        $newItem[$sumName] = "合计";
        array_push($data["list"], $newItem);

        $data["bold"] = (string)(sizeof($idArr)+2);
//        $data["red"] = (string)(sizeof($idArr)+2);

//        $fileName = "定制产品报表_".date('Ymd',time());
        $fileName = "团购订单报表_" . date('Ymd',time());

        exportExcel($data, $fileName);
    }
}